<?php
// @formatter:off
/**
 * @file index.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-03-30 18:08:43 +0000 (Sat, 30 Mar 2013) $
 *
 * $Id: index.php 59 2013-03-30 18:08:43Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/install/index.php $
 */

$wcROOTDIR = dirname(dirname(__FILE__));

require_once "{$wcROOTDIR}/config/config.php";

if($wcDefaults['installed']) {
	die("<pre>Intallation disabled by an already installed configuration file.</pre>");
}

$wciSTEP = 0;
if(is_readable($wcPaths['install-tmps']['step'])) {
	include $wcPaths['install-tmps']['step'];
}

define("__INSTALLER__", "__INSTALLER__");

$isOkToContinue    = true;
$isOkToReload      = true;
$isFastReload      = false;
$isOkToRemoveSteps = false;
$stepIncluded      = false;
?>
<html>
	<head>
		<title>Install Process</title>
		<link rel="stylesheet" type="text/css" href="install.css"/>
		<script type="text/javascript" src="../includes/jquery/<?php echo $wcVersions['jquery']; ?>/jquery-<?php echo $wcVersions['jquery']; ?>.min.js"></script>
		<script type="text/javascript" src="install.js"></script>
	</head>
	<body>
<?php
if(is_readable("{$wcPaths['install-directory']}/steps/{$wciSTEP}.php")) {
	echo "\t\t<span class=\"PageTitle\">Install Process: Step {$wciSTEP}</span>\n";
} else {
	echo "\t\t<span class=\"PageTitle\">Install Process: Final Step</span>\n";
}

$wciStepCheck = 0;
$hasMoreSteps = true;
while($hasMoreSteps) {
	$stepPath = "{$wcPaths['install-directory']}/steps/{$wciStepCheck}.php";
	if(is_readable($stepPath)) {
		echo "\t\t<div class=\"Section\">\n";
		echo "\t\t\t<span class=\"Title\">Step {$wciStepCheck}</span>\n";
		echo "\t\t\t<div class=\"Paragraph\">\n";

		if($wciSTEP == $wciStepCheck) {
			include $stepPath;
			$stepIncluded = true;
		} elseif($wciSTEP < $wciStepCheck) {
			echo "\t\t\t\t<p>[FL:Awaiting previous steps...]</p>\n";
		} elseif($wciSTEP > $wciStepCheck) {
			echo "\t\t\t\t<p>[OK:Step Completed]</p>\n";
		}

		echo "\t\t\t</div>\n";
		echo "\t\t</div>\n";
	} else {
		break;
	}
	$wciStepCheck++;
}

echo "\t\t<div class=\"Section\">\n";
echo "\t\t\t<span class=\"Title\">Final Step</span>\n";
echo "\t\t\t<div class=\"Paragraph\">\n";
if(!$stepIncluded) {
	include "{$wcPaths['install-directory']}/steps/finalize.php";
}
echo "\t\t\t</div>\n";
echo "\t\t</div>\n";

if($isOkToContinue) {
	file_put_contents($wcPaths['install-tmps']['step'], "<?php\n\$wciSTEP = ".($wciSTEP+1).";\n?".">\n");
}

if(!$isOkToRemoveSteps && $isOkToReload) {
	if(!$isFastReload) {
		$secs = $isOkToContinue? 1 : 5;
		echo "\t\t<script type=\"text/javascript\">\n";
		echo "\t\t\t$(function(){\n";
		echo "\t\t\t\t$(\"body\").prepend('<span class=\"Warning\">This instance will automatically reload. Please wait in {$secs}secs...</span>');\n";
		echo "\t\t\t\twindow.setTimeout('location.reload()', {$secs}000);\n";
		echo "\t\t\t});\n";
		echo "\t\t</script>\n";
	} else {
		echo "\t\t<script type=\"text/javascript\">\n";
		echo "\t\t\t$(function(){\n";
		echo "\t\t\t\tlocation.reload();\n";
		echo "\t\t\t});\n";
		echo "\t\t</script>\n";
	}
} elseif($isOkToRemoveSteps) {
	@unlink($wcPaths['install-tmps']['step']);
	@unlink($wcPaths['install-tmps']['cron-check']);
}

?>
	</body>
</html>
